﻿#include "history.h"
#include "ui_history.h"
#include <QDebug>

History::History(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::History)
{
    ui->setupUi(this);

    this->initScene(); //初始化窗口
    this->hasHistory = false;
    this->sureClear = new SureClear;
}

History::~History()
{
    delete ui;
}

void History::initScene()
{
    setWindowTitle(GAME_NAME); //设置窗口标题
//    setWindowIcon(QIcon(GAME_ICON_)); //设置窗口图标
    //this->initQLabel(); //初始化标签
}

void History::saveToFile()
{
    //实现不同用户使用不同的文件
    QString fileName;
    fileName = "./";
    fileName += this->userName;
    fileName += "History.txt";

    QFile file(fileName);
    file.open(QIODevice::WriteOnly|QIODevice::Truncate|QIODevice::Text);
    QTextStream out(&file);
    out.setCodec("UTF-8");
    for(int i=0;i<this->temp.size();i++){
       if(i==0){
            out<<this->temp.at(i)->time<<" "<<this->temp.at(i)->score;
            continue;
       }
            out<<"\n"<<this->temp.at(i)->time<<" "<<this->temp.at(i)->score;
        }
        file.close();
}

void History::readFile()
{
    this->temp.clear(); //清空链表
    QString fileName;
    fileName = "./";
    fileName += this->userName;
    fileName += "History.txt";
    //读取文件
    QFile file (fileName);
    if(file.open(QIODevice::ReadOnly| QIODevice::Text )){
        this->hasHistory = true;

        QByteArray t=file.readAll();
        QString alltext=QString(t);
        QStringList list=alltext.split("\n");
        for (int i=0; i<list.size() ;i++ ) {
            QStringList temp=list[i].split(" ");
            if(temp.size()>1){
                this->temp.append(new His(temp[0],temp[1]));
            }
        }
        file.close();
        //往user中临时存数据
        //users.append(new User("123123","123123"));
    }


}

void History::zhiZuo()
{
    this->sortPaiHang(); //排序历史记录
    for(int i = 0; i < this->temp.size(); i++){
        lb[i * 3 + 0] = new QLabel(this);
        lb[i * 3 + 0]->setText(QString::number(i + 1));
        lb[i * 3 + 0]->setGeometry(10, 60 + 20 * i, 288, 15);
        lb[i * 3 + 1] = new QLabel(this);
        lb[i * 3 + 1]->setText(this->tempShuZu[i]->time);
        lb[i * 3 + 1]->setGeometry(240, 60 + 20 * i, 288, 15);
        lb[i * 3 + 2] = new QLabel(this);
        lb[i * 3 + 2]->setText(this->tempShuZu[i]->score);
        lb[i * 3 + 2]->setGeometry(480, 60 + 20 * i, 288, 15);
    }
}

void History::clearIt()
{
    for(int i = 0; i < this->temp.size(); i++){
        lb[i * 3 + 0]->clear();
        lb[i * 3 + 1]->clear();
        lb[i * 3 + 2]->clear();
    }
}

void History::sortPaiHang()
{
    for(int i = 0; i < this->temp.size(); i++){
        this->tempShuZu[i] = new His(temp.at(i)->time, temp.at(i)->score);
    }

    int k = 0;
    for(int i = 0; i < this->temp.size() - 1; i++){
        k = i;
        for(int j = i + 1; j < this->temp.size(); j++){
            if(tempShuZu[k]->score.compare(tempShuZu[j]->score) < 0){
                k = j; //记录最大数的下标
            }
        }

        if(k != i){
            His* t;
            t = new His(tempShuZu[i]->time, tempShuZu[i]->score);
            tempShuZu[i] = new His(tempShuZu[k]->time, tempShuZu[k]->score);
            tempShuZu[k] = new His(t->time, t->score);
        }
    }

}

//清空链表和文件
void History::clearAll()
{
    this->temp.clear(); //清空链表
    this->saveToFile(); //更新文件
}

void History::recShowHistory()
{
    this->readFile();
    if(this->temp.size() == 0){
        this->hasHistory = false; //使得我界面不被关闭
        QMessageBox::warning(NULL, GAME_NAME, "暂无历史记录！");
    }else if(this->hasHistory){
        this->zhiZuo(); //制作历史记录
        this->show();
    }
}

//接收信号更新历史记录
void History::recUpdateHistory(QString score)
{
    this->readFile();

    QDateTime dateTime = QDateTime::currentDateTime(); //获取当前日期
    QTime time = QTime::currentTime(); //获取当前时间
    this->temp.append(new His(dateTime.toString("yyyy年MM月dd日") + time.toString("hh:mm:ss"), score));
    this->saveToFile(); //更新文件中的信息
}

//是清空
void History::recYesClear()
{
    this->clearIt(); //清空标签
    this->clearAll(); //清空链表
    emit this->showMe(); //显示我界面
    this->hide();
}

//否清空
void History::recNoClear()
{
    
}

void History::recNameToHistory(QString userName, QString nickName)
{
    this->userName = userName;
    this->nickName = nickName;
    ui->nickName->setText(this->nickName);
}

void History::on_back_clicked()
{
    this->clearIt(); //清空历史记录
    emit this->showMe(); //显示我界面
    this->hide();
}

void History::on_clearAll_clicked()
{
    emit this->sendShowSureClear();
}
